package com.glela.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.glela.goods.model.ReceiveGoodsCache;
import com.glela.goods.model.request.RequestGYBaseModel;
import com.glela.goods.model.vo.GoodsSendVo;
import com.glela.goods.service.ReceiveGoodsService;
import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.response.ResponseBaseModel;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description: 接收贡云商品接口
 * @Author: zhangrenjie
 * @Date: 2018/1/31 15:17
 *
 */
@Controller
@RequestMapping("/receiveGoodsController")
public class ReceiveGoodsController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(ReceiveGoodsController.class);
    @Autowired
    private ReceiveGoodsService receiveGoodsService;

    /**
     * @Description: 接收贡云商品推送接口
     * @Author: zhangrenjie
     * @Date: 2018/1/31 15:28
     *
     */
    @RequestMapping(value = "/receiveGoodsData", method = RequestMethod.POST, consumes = "application/json;charset=utf8")
    @ResponseBody
    public ResponseEntity<?> receiveGoodsData(@RequestBody RequestGYBaseModel model) {
        try{
            return receiveGoodsService.receiveGoodsData(model);
        } catch (Exception e) {
            logger.error("接收贡云商品信息异常", e);
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
        }
    }
    /**
     * @Description: 自动任务同步商品相关信息
     * @Author: zhangrenjie
     * @Date: 2018/1/31 21:15
     *
     */
    @RequestMapping(value = "/sysGoodsDataToStore", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> sysGoodsDataToStore(HttpServletRequest request) {
        try{
//            return receiveGoodsService.sysGoodsDataToStore();
            //查询缓存表根据类型开始初始化商品信息
            ReceiveGoodsCache bean = new ReceiveGoodsCache();
            bean.setStatus(new Byte("0"));//未同步
//            List<ReceiveGoodsCache> cacheList = receiveGoodsCacheMapper.selectAll(bean);
            List<ReceiveGoodsCache> cacheList = receiveGoodsService.selectAllReceiveGoodsCache(bean);
            List<GoodsSendVo> goodsSendVoList = null;
            if (!CollectionUtils.isEmpty(cacheList)) {
                int i = 0;
                for (ReceiveGoodsCache receiveGoodsCache : cacheList) {
                    if (i>5) {//每次只读取5个去初始化
                        break;
                    }
                    try {
                        goodsSendVoList = JSON.parseObject(receiveGoodsCache.getData(),new TypeReference<List<GoodsSendVo>>(){});
                    } catch (Exception e ) {
                        logger.error("Goods商品："+receiveGoodsCache.getGoodsCode()+"JSON Parsing Error！reason ："+e.getMessage());
                        e.printStackTrace();
                    }
                    if (goodsSendVoList != null && !goodsSendVoList.isEmpty()) {
                        try {
                            receiveGoodsService.addInitGoods(goodsSendVoList,receiveGoodsCache.getType());
//                            receiveGoodsService.addInitGoodsTrancation(goodsSendVoList,receiveGoodsCache.getType());
                        } catch (Exception e) {
                            logger.error("Goods商品："+receiveGoodsCache.getGoodsCode()+"Initialization failed ! reason："+e.getMessage());
                            e.printStackTrace();
                            continue;//发生异常直接跳过
                        }
                        //更新缓存表状态
                        ReceiveGoodsCache receiveGood = new ReceiveGoodsCache();
                        receiveGood.setId(receiveGoodsCache.getId());
                        receiveGood.setStatus(new Byte("1"));
//                        receiveGoodsCacheMapper.updateByPrimaryKeySelective(receiveGood);
                        receiveGoodsService.updateByPrimaryKeySelective(receiveGood);
                        Thread.sleep(200);
                    }
                    i++;
                }
            }
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
        } catch (Exception e) {
            logger.error("初始化商品Goods Information Error", e);
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
        }
    }
    /**
     * @Description: 自动任务同步商品信息到WMS
     * @Author: zhangrenjie
     * @Date: 2018/2/1 17:58
     *
     */
    @RequestMapping(value = "/sysProductToWMS", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> sysProductToWMS(HttpServletRequest request) {
        try{
            return receiveGoodsService.sysProductToWMS();
        } catch (Exception e) {
            logger.error("同步WMS商品信息Product Error ", e);
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
        }
    }
    /**
     * @Description: 处理异常数据接口
     * @Author: zhangrenjie
     * @Date: 2018/4/12 10:34
     *
     */
    @RequestMapping(value = "/updateProductSpecInfoData", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<?> updateGoodsData(HttpServletRequest request) {
        try{

            receiveGoodsService.updateProductData(null);
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C10000), HttpStatus.OK);
        } catch (Exception e) {
            logger.error("更新货品Product Information Error", e);
            return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
        }
    }


}
